// 请求器实例，挂载于window._request
import axios from 'axios';
import qs from 'qs';
import { prodApi, devApi } from '../../my-config';
import { login } from '../utils/common'

var config = {
	timeout: 7000,
	headers: {
		'Content-Type': 'application/x-www-form-urlencoded',
        'apppt':4,
	},
	transformRequest: qs.stringify,
};

var axiosInstance = axios.create({
	baseURL: process.env.NODE_ENV === 'production' ? prodApi : devApi,
	...config,
});

axiosInstance.interceptors.request.use(requestDataHandler);
axiosInstance.interceptors.response.use(responseDataHandler);

// 拦截器的写法请参考 @see https://www.kancloud.cn/yunye/axios/234845#_395

// 请求拦截器
function requestDataHandler(req) {
	let token = localStorage.getItem('token');
	if (token) {
		req.headers['token'] = token;
	}
	let testlist;
	return req;
}

// 响应拦截器
async function responseDataHandler(res) {
	//用户信息错误
	if (res.data.code == 101 || res.data.code == 102 || res.data.code == 103) {
		this.$message('用户信息错误');
		// await login();
		return false
	}
	return res.data;
}

export default axiosInstance;
